<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Photo;

use Storage;
use Image;

class PhotosController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //数据验证
        $this->validate($request,[
            'photo' => 'required',
        ]);

        //生成路径，图片存储
        /* $name = "photo".time();
         $src = "images/album/photos/". $name .".jpg";
         Image::make($request->photo)->save(public_path($src));*/

        //批量上传图片

        foreach ($request->photo as $key => $value) {
            //生成路径，图片存储
            $prefix = "photo".mt_rand(1,1000);
            $Origname = $value->getClientOriginalName();
            $name = $prefix.$Origname;
            $src = "images/album/photos/".$name;
            $realPath = $value->getRealPath();
            Storage::disk('uploads')->put($src,file_get_contents($realPath));

            //如果输入了标题则存储否则存储之前生成的默认标题
            if($request->has('name')){
                $name = $request->name;
            }

            //存储数据
            $photo = Photo::create([
                'album_id' => $request->album_id,
                'name' => $name,
                'descr' => $request->intro,
                'src' => "/" . $src,
            ]);
        }
        //返回
        session()->flash('success', 'Upload Successful');
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //更新
        $photo = Photo::findOrFail($id);
        $photo->update([
            'name' => $request->name,
            'descr' => $request->intro,
        ]);

        //返回
        session()->flash('success', 'Edit Successful');
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        $photo = Photo::findOrFail($id);
        $photo->delete();

        //返回
        session()->flash('success', 'Delete Successful');
        return back();
    }
}
